<?php

namespace core;

use core\lib\container;
use core\lib\provider;

class ying {
    public static $classMap = [];

    /**
     * 初始化入口方法
     * 路由匹配
     */
    static function run() {
        \core\lib\log::init();
        $route = new \core\lib\route();
        $ctrl = $route->ctrl;
        $action = $route->action;
        $ctrl_file = APP . '/ctrl/' . $ctrl . 'Ctrl.php';
        $ctrl_class = '\\' . MODULE . '\ctrl\\' . $ctrl . 'Ctrl';
        
        if (is_file($ctrl_file)) {
            $container = new container();
            provider::register($container);
            // 服务启动
            $ctrl_obj = $container->build($ctrl_class);
            $ctrl_obj->$action();
        }
    }

    /**
     * 自动加载方法
     */
    static function load($class) {
        if (isset($classMap[$class])) {
            return true;
        } else {
            $class = str_replace('\\', '/', $class);
            $file = YING . $class . '.php';
            if (is_file($file)) {
                include $file;
                self::$classMap[$class] = $file;
            } else {
                return false;
            }
        }
    }
}